package com.guapi.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import com.guapi.annotation.SelfController;
import com.guapi.dao.Mail;
import com.guapi.dao.ResponseData;
import com.guapi.dao.User;
import com.guapi.exception.SelfException;
import com.guapi.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author 瓜皮哦
 */
@SelfController
@RequestMapping("/mail")
@ResponseBody
public class MailController extends BaseController {

    @Autowired
    private MailService mailService;

    /**
     * 上传邮件
     *
     * @param request
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public ResponseData uploadHeader(HttpServletRequest request,
                                     @RequestParam("file") MultipartFile file,
                                     @RequestParam("detail") String detail,
                                     @RequestParam("remark") String remark) {
        responseData = new ResponseData();
        Mail mail = new Mail();
        User loginUser = getLoginUser();
        try {
            byte[] bytes = file.getBytes();
            InputStream is = file.getInputStream();
            mail.setName(file.getOriginalFilename());
            mail.setDetail(detail);
            mail.setRemark(remark);
            mail.setCreateBy(String.valueOf(loginUser.getId()));
            mail.setEnable(ISENABLE);
            FileUtil.writeFromStream(is, resourcePath + "\\static" + "\\" + file.getOriginalFilename());
            responseData.setData("filename", file.getOriginalFilename());
            responseData.setSuccess(true);
            mailService.saveOrUpdate(mail);
            return responseData;
        } catch (IOException e) {
            e.printStackTrace();
            throw new SelfException("文件读取异常，请稍后重试");
        }
    }

    /**
     * 获取数据库邮件列表
     *
     * @param request
     * @param data
     * @return
     */
    @RequestMapping("/list")
    public ResponseData getMailList(HttpServletRequest request, @RequestBody Map data) {
        responseData = new ResponseData();
        List<Mail> mailList;

        String searchName = "";
        String searchDetail = "";
        Map searchStr = (Map) data.get("searchStr");
        if (searchStr != null) {
            searchName = (String) searchStr.get("name");
            searchDetail = (String) searchStr.get("detail");
        }
        startPage(data);

        if (StrUtil.isAllEmpty(searchName, searchDetail)) {
            mailList = mailService.list();
        } else {
            Mail mail = new Mail();
            if (StrUtil.isNotEmpty(searchName)) {
                mail.setName(searchName);
            }
            if (StrUtil.isNotEmpty(searchDetail)) {
                mail.setDetail(searchDetail);
            }
            mailList = mailService.getByFuzzy(mail);
        }
        responseData.setRows(mailList);
        responseData.setSuccess(true);
        return responseData;
    }

    /**
     * 展示邮件
     *
     * @param request
     * @param mail
     * @return
     */
    @RequestMapping("/show")
    public ResponseData showMailDetail(HttpServletRequest request, @RequestBody Mail mail) {
        String filePath = "\\static\\" + mail.getName();
        responseData = new ResponseData();
        responseData.setData("content", getResource(filePath));
        responseData.setSuccess(true);
        return responseData;
    }

    /**
     * 批量发送邮件
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping("/send")
    public ResponseData sendMails(HttpServletRequest request, @RequestBody Map map) {
        responseData = new ResponseData();
        List<String> mails = (List<String>) map.get("mails");
        String mailName = (String) map.get("mailName");
        String subject = String.valueOf(map.get("subject"));
        MailUtil.send(mails,
                subject,
                getResource("static/"+mailName),
                true);
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping("/delete")
    public ResponseData deleteMail(HttpServletRequest request, @RequestBody Mail mail) {
        responseData = new ResponseData();
        mailService.deleteById(mail);
        //文件不删除了吧
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping("/sendOne")
    public ResponseData sendMail(HttpServletRequest request, @RequestBody Map data) {
        responseData = new ResponseData();
        MailUtil.send((String) data.get("toMail"),
                (String) data.get("subject"),
                (String) data.get("content"),
                false);
        responseData.setSuccess(true);
        return responseData;
    }
}

